package com.riddle.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.riddle.model.Permission;
import com.riddle.model.Role;
import com.riddle.service.BaseService;
import com.riddle.service.RoleService;
import com.riddle.util.JsonUtil;
import com.riddle.vmodel.QueryModel;
import com.riddle.vmodel.VMsg;
import com.riddle.vmodel.VTreeModel;
import com.riddle.vmodel.ValuePair;

@Controller
@RequestMapping("/admin/role")
public class RoleController extends BaseController<Role> {

	@RequestMapping()
	public String index() {
		return "admin/role/index";
	}
	
	@Resource
	private RoleService roleService;
	
	protected BaseService getService() {
		return roleService;
	}
	
	@RequestMapping(value="/list", produces="application/json;charset=utf-8")
	@ResponseBody
	public String list(QueryModel queryModel) {
		return super.list(queryModel);
	}
	
	@RequestMapping("/add")
	public String add() {
		return "admin/role/add";
	}
	
	@RequestMapping(value="/addmodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String addModel(Role role) {
		return super.addModel(role);
	}
	
	@RequestMapping("/modify")
	public ModelAndView modify(Long id) {
		Role role = roleService.query(id);
		return new ModelAndView("admin/role/modify", "role", role);
	}
	
	@RequestMapping(value="/modifymodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String modifyModel(Role role) {
		return super.modifyModel(role);
	}
	
	@RequestMapping(value="/delete", produces="application/json;charset=utf-8")
	@ResponseBody
	public String delete(Long id) {
		return super.delete(id);
	}
	
	@RequestMapping(value="/getparentmodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String getParentModel() {
		return super.getParentModel();
	}
	
	@RequestMapping("/assign")
	public ModelAndView assign(Long id) {
		Role role = roleService.getRoleWithPermission(id);
		role.getPermissionsStr();
		return new ModelAndView("admin/role/assign", "role", role);
	}
	
	@RequestMapping(value="/assignpermission", produces="application/json;charset=utf-8")
	@ResponseBody
	public String assignPermission(Long id, String permissions) {
		List<ValuePair<Long, String>> list = new ArrayList<ValuePair<Long, String>>();
		String[] strs = permissions.split(",");
		for(String s : strs) {
			ValuePair<Long, String> value = new ValuePair<Long, String>();
			value.setValue1(id);
			value.setValue2(s);
			list.add(value);
		}
		roleService.deleteRolePermission(id);
		roleService.saveRolePermission(list);
		VMsg msg = new VMsg();
		msg.setMessage("success");
		return JsonUtil.toJson(msg);
	}
	
	@RequestMapping(value="/tree", produces="application/json;charset=utf-8")
	@ResponseBody
	public String tree() {
		List<Role> list = roleService.getRoleTree();
		List<VTreeModel> res = VTreeModel.toVTreeModel(list);
		return JsonUtil.toJson(res);
	}
}

